﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Intorise.Net;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            // Security.Init();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form2());
        }

        //读取数据的方法
        public static List<LotteryData> GetData(DateTime date)
        {
            List<LotteryData> list = new List<LotteryData>();

            NetTools tools = new NetTools();
            CookieContainer cookie = new CookieContainer();
            var request = tools.SendRequest("http://data.shishicai.cn/cqssc/haoma/", "GET", cookie);
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
            var response = (HttpWebResponse)request.GetResponse();
            cookie.Add(response.Cookies);

            var html = tools.GetResponseHtmlGzip(response);
            //Console.Write(html);

            //request = tools.SendRequest("http://www.shishicai.cn/handler/kuaikai/command.ashx", "POST", cookie);
            //request.Referer = "http://www.shishicai.cn/cqssc/touzhu/";
            //request.Accept = "*/*";
            ////request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip,deflate,sdch";
            //request.Headers.Add("X-Requested-With", "XMLHttpRequest");
            //request.Headers[HttpRequestHeader.Cookie] = "ssc_user_LandingPage=http%3a%2f%2fdata.shishicai.cn%2fcqssc%2fhaoma%2f2014-06-05%2f; Html_v_54=7d8c_63537536701175; Hm_lvt_cad2e9c6544a1e8f06862d019ce44f71=1401908426; Hm_lpvt_cad2e9c6544a1e8f06862d019ce44f71=1401911184; Html_v_4=e834_63537537230347; ssc_user_RegEnterPage=http%3a%2f%2fwww.shishicai.cn%2fcqssc%2ftouzhu%2f; bet_kuaikai_4_dPlayType=3";
            //tools.PostData("d=%7B%22c%22%3A4%2C%22p%22%3A%5B4%5D%7D", request, Encoding.ASCII);
            //response = (HttpWebResponse)request.GetResponse();
            //html = tools.GetResponseHtmlGzip(response);
            //Console.Write(html);

            request = tools.SendRequest("http://data.shishicai.cn/handler/kuaikai/data.ashx", "POST", cookie);
            request.Referer = "http://data.shishicai.cn/cqssc/haoma/";
            request.Accept = "*/*";
            request.Headers.Add("X-Requested-With", "XMLHttpRequest");
            tools.PostData("lottery=4&date=" + date.ToString("yyyy-MM-dd"), request, Encoding.ASCII);//2014-06-02为要获取的日期

            response = (HttpWebResponse)request.GetResponse();
            html = tools.GetResponseHtmlGzip(response);
            //Console.Write(html);//html为获取到的彩票开奖数据

            Regex regex = new Regex("\".[^\"]+\"");
            var matches = regex.Matches(html);

            foreach (Match m in matches)
            {
                Console.WriteLine(m.Value);
                var data = m.Value.Replace("\"", "").Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                if (data.Length == 3)
                {
                    list.Add(new LotteryData { SN = data[0], Numbers = data[1], Datetime = data[2] });
                }
            }
            string code = matches[matches.Count - 1].Value.Replace("\"", "");
            code = code.Substring(0, code.IndexOf('_')).Replace(",", "");
            //Console.WriteLine("code:" + code);
            //Console.ReadLine();
            StringBuilder sb = new StringBuilder();
            list.ForEach(x =>
            {
                sb.Clear();
                for (int i = 0; i < x.Numbers.Length; i++)
                {
                    sb.Append(code.IndexOf(x.Numbers[i]).ToString());
                }
                x.Numbers = sb.ToString();
            });
            list.ForEach(x =>
            {
                Console.WriteLine(x.Numbers);
            });
            //Console.ReadLine();
            //list.Reverse();
            //int index = 1;
            //list.ForEach(x =>
            //{
            //    x.ID = index++;
            //});
            return list;
        }
    }
}